package com.example.spring.spring.register.bean;

import com.example.spring.spring.register.bean.ExampleBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: orange
 * @create: 2023-08-05 23:27
 * @description:
 */
@RestController
@RequestMapping("/register/bean")
public class RegisterBeanController implements ApplicationContextAware {

    private GenericApplicationContext generic;

    @GetMapping("/example")
    public String registerBean() {
        if (generic == null) {
            return "上下文不支持！";
        }
        generic.registerBean("registerExampleBean", ExampleBean.class);
        ExampleBean exampleBean = (ExampleBean) generic.getBean("ExampleBean");

        return exampleBean.getBeanData().getName();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof GenericApplicationContext) {
            generic = (GenericApplicationContext) applicationContext;
        }
    }
}
